<template>
  <div class="container">
    <div class="header">
      <div>
        <!-- navbar -->
        <navbar :navTitle="跳蚤市场"></navbar>
        <!-- 筛选 -->
        <div class="choicetop">
          <div class="choicelf">
            <div :class="user_type==1&&'active'"
                 @click="changeUertype(1)">我要买</div>
            <div :class="user_type==2&&'active'"
                 @click="changeUertype(2)">我要卖</div>
          </div>
          <div class="choicert">
            <div @click="drawerShow=true">
              <img v-if="ossUrl"
                   :src="ossUrl+'/mp_3.2.4/loudou.png'"
                   alt />
              筛选
            </div>
            <div class="line"></div>
            <div @click="toOrder">
              <img v-if="ossUrl"
                   :src="ossUrl+'/mp_3.2.4/order.png'"
                   alt />
              订单
            </div>
          </div>
        </div>
        <div class="choicebottom">
          <div :class="type==1&&'actived'"
               @click="changeType(1)">卡</div>
          <div :class="type==2&&'actived'"
               @click="changeType(2)">优惠券</div>
        </div>
      </div>
      <!-- 滚动区域 -- 卡/券列表 -->
      <div class="list">
        <swiper :current="current"
                @change="changeCurrent">
          <block>
            <swiper-item>
              <div class="cardbox">
                <scroll-view scroll-y
                             scroll-with-animation
                             style="height:100%"
                             @scrolltolower="loadmore">
                  <!-- 卡 -->
                  <card v-if="type==1"
                        :cardArr="cardArr"
                        :user_type="user_type"
                        @cancle="cancle"></card>
                  <!-- 券 -->
                  <coupon v-if="type==2"
                          :couponArr="couponArr"
                          :user_type="user_type"
                          @cancle="cancle"></coupon>
                </scroll-view>
              </div>
            </swiper-item>
          </block>
          <block>
            <swiper-item>
              <div class="couponbox">
                <scroll-view scroll-y
                             scroll-with-animation
                             style="height:100%"
                             @scrolltolower="loadmore">
                  <!-- 卡 -->
                  <card v-if="type==1"
                        :cardArr="cardArr"
                        :user_type="user_type"
                        @cancle="cancle"></card>
                  <!-- 券 -->
                  <coupon v-if="type==2"
                          :couponArr="couponArr"
                          :user_type="user_type"
                          @cancle="cancle"></coupon>
                </scroll-view>
              </div>
            </swiper-item>
          </block>
        </swiper>
      </div>
    </div>
    <!-- drawer -->
    <div class="drawer-mask"
         :class="drawerShow&&'.drawer-show'"
         @click="drawerShow=false">
      <div class="drawer-container"
           v-if="drawerShow"
           :style="{paddingTop:height+'px'}"
           @click.stop>
        <div class="drawer-title">价格区间</div>
        <div class="drawer-content">
          <input type="number"
                 v-model="sale_price_min"
                 placeholder-style="color:#999999"
                 placeholder="最低价"
                 maxlength="7" />
          <div class="reduce"></div>
          <input type="number"
                 v-model="sale_price_max"
                 placeholder-style="color:#999999"
                 placeholder="最高价"
                 maxlength="7" />
        </div>
        <div class="drawer-title">成交率</div>
        <div class="drawer-content">
          <input type="number"
                 v-model="deal_rate_min"
                 placeholder-style="color:#999999"
                 placeholder="最低"
                 maxlength="7" />
          <div class="reduce"></div>
          <input type="number"
                 v-model="deal_rate_max"
                 placeholder-style="color:#999999"
                 placeholder="最高"
                 maxlength="7" />
        </div>
        <!-- 底部 -->
        <div class="btnbox"
             :class="iPhoneX&&'iphonex'">
          <div class="safearea">
            <div class="reset"
                 @click="reset">重置</div>
            <div class="screen"
                 @click="screen">筛选</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import common from "@/assets/js/mmk_common.js";
import navbar from "@/components/NavBar/index";
import card from "@/components/card/flea_card";
import coupon from "@/components/coupon/flea_coupon";
export default {
  components: {
    navbar,
    card,
    coupon
  },
  data () {
    return {
      ossUrl: common.image_resource, //oss图片
      imgUrl: common.image_response, //http图片
      iPhoneX: false, //是否是iPhoneX
      current: 0, //当前滑块
      user_type: 1, //1我要买2我要卖
      type: 1, //1卡2券
      keyword: "", //搜索关键字
      page: 1, //当前页
      sale_price_min: "", //	最低售价
      sale_price_max: "", //最高售价
      deal_rate_min: "", //最低成交率
      deal_rate_max: "", //最高成交率
      height: 64,
      cardArr: [], //卡列表
      couponArr: [], //券列表
      isbottom: false,
      drawerShow: false //筛选弹窗
    };
  },
  onLoad (options) {
    let obj = wx.getMenuButtonBoundingClientRect();
    this.height = obj.top + obj.height + 8;
    //获取系统信息
    let that = this;
    wx.getSystemInfo({
      success: res => {
        this.windowWidth = res.windowWidth;
        if (res.model.search("iPhone X") != -1) {
          that.iPhoneX = true;
        }
      }
    });
    // 获取传参
    if (options.type) {
      this.type = options.type;
    }
    if (options.user_type) {
      this.user_type = options.user_type;
      this.current = this.user_type - 1;
    }
    // 获取列表
    this.getList();
  },
  onShow () {
    this.getList();
  },
  methods: {
    // 切换买/卖
    changeUertype (val) {
      this.current = val - 1;
    },
    // 切换type 卡/券
    changeType (val) {
      this.type = val;
      this.page = 1;
      this.getList();
    },
    // 切换滑块
    changeCurrent (e) {
      this.current = e.mp.detail.current;
      this.user_type = this.current + 1;
      this.type = 1;
      this.cardArr = []; //卡列表
      this.couponArr = []; //券列表
      this.page = 1;
      this.getList();
    },
    // 点击订单
    toOrder () {
      wx.navigateTo({
        url: "/pages/ct_store/market_order_list/main"
      });
    },
    // 获取列表
    getList () {
      if (this.isbottom) {
        return false;
      }
      common.mmk_Loading(0);
      common.fly_post(
        "api/v4_5/c2c/user_card_ticket_list",
        {
          user_type: this.user_type,
          type: this.type,
          keyword: this.keyword,
          size: 10,
          page: this.page,
          sale_price_min: this.sale_price_min,
          sale_price_max: this.sale_price_max,
          deal_rate_min: this.deal_rate_min,
          deal_rate_max: this.deal_rate_max
        },
        response => {
          common.mmk_Loading(1);
          let res = response.data;
          if (res.status_code == 0) {
            switch (this.type) {
              case 1: //卡
                if (this.page == 1) {
                  this.cardArr = res.data.data || [];
                } else if (this.page > 1 && res.data.data.length > 0) {
                  this.cardArr.push(...res.data.data);
                } else if (this.page > 1 && res.data.data.length == 0) {
                  this.isbottom = true;
                }
                break;
              case 2: //券
                let arr = res.data.data || [];
                arr.map(item => {
                  item.validity_start_time =
                    (item.validity_start_time &&
                      new Date(item.validity_start_time * 1000).Format(
                        "yyyy.MM.dd"
                      )) ||
                    "";
                  item.validity_end_time =
                    (item.validity_end_time &&
                      new Date(item.validity_end_time * 1000).Format(
                        "yyyy.MM.dd"
                      )) ||
                    "";
                  return item;
                });
                if (this.page == 1) {
                  this.couponArr = arr;
                } else if (this.page > 1 && arr.length > 0) {
                  this.couponArr.push(...arr);
                } else if (this.page > 1 && arr.length == 0) {
                  this.isbottom = true;
                }
                break;
            }
          } else {
            common.mmk_Loading(2, res.message);
          }
        }
      );
    },
    // 加载更多
    loadMore () {
      if (this.isbottom) {
        return false;
      }
      this.page++;
      this.getList();
    },
    // 重置
    reset () {
      this.drawerShow = false;
      this.page = 1; //当前页
      this.sale_price_min = ""; //	最低售价
      this.sale_price_max = ""; //最高售价
      this.deal_rate_min = ""; //最低成交率
      this.deal_rate_max = ""; //最高成交率
      this.getList();
    },
    // 筛选
    screen () {
      this.page = 1; //当前页
      this.drawerShow = false;
      this.getList();
    },
    // 取消挂售
    cancle (item) {
      common.mmk_Loading(0);
      common.fly_post(
        "api/v4_5/c2c/hangon",
        {
          opt: 2,
          type: item.type,
          id: item.sale_id,
          mobile: "",
          code: "",
          sale_price: item.sale_price
        },
        response => {
          common.mmk_Loading(1);
          let res = response.data;
          if (res.status_code == 0) {
            common.mmk_Loading(2, res.message);
            this.getList();
          } else {
            common.mmk_Loading(2, res.message);
          }
        }
      );
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  background: #f3f4f6;
  box-sizing: border-box;
  .header {
    position: sticky;
    top: 0;
    background: url("https://img.ameimeika.com/h5_images/mp_images/mp_3.2.4/jumpsao.png")
      no-repeat;
    background-size: 100% auto;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    // 筛选
    .choicetop {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 19px 12px;
      box-sizing: border-box;
      .choicelf {
        display: flex;
        align-items: center;
        box-sizing: border-box;
        div {
          font-size: 16px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: rgba(255, 255, 255, 1);
          line-height: 22px;
          margin-right: 27px;
          margin-bottom: 10px;
          transition: all 0.2;
          &.active {
            font-size: 20px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: rgba(255, 255, 255, 1);
            line-height: 28px;
            position: relative;
            &::before {
              content: "";
              width: 38px;
              height: 4px;
              background: rgba(255, 255, 255, 1);
              border-radius: 2px;
              position: absolute;
              bottom: -10px;
              left: 50%;
              transform: translateX(-50%);
            }
          }
        }
      }
      .choicert {
        display: flex;
        align-items: center;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: rgba(255, 255, 255, 1);
        line-height: 20px;
        div {
          &.line {
            width: 1px;
            height: 10px;
            background: #ffffff;
            margin: 0 12px;
          }
          img {
            width: 14px;
            height: 14px;
            vertical-align: middle;
          }
        }
      }
    }
    .choicebottom {
      padding: 0 12px 16px;
      display: flex;
      align-items: center;
      box-sizing: border-box;
      div {
        width: 90px;
        height: 24px;
        line-height: 24px;
        margin-right: 14px;
        box-sizing: border-box;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 12px;
        text-align: center;
        font-size: 16px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: rgba(255, 255, 255, 0.4);
        &.actived {
          background: rgba(255, 255, 255, 0.4);
          color: #ffffff;
        }
      }
    }
    // 滚动区域
    .list {
      width: 100%;
      flex: 1;
      z-index: 10;
      swiper {
        width: 100%;
        height: 100%;
      }
    }
    .cardbox,
    .couponbox {
      width: 100%;
      height: 100%;
      padding: 0 12px;
      box-sizing: border-box;
    }
  }
  // drawer
  .drawer-mask {
    visibility: hidden;
    opacity: 0;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 8888;
    background: rgba(0, 0, 0, 0.6);
    transition: all 0.3s ease-in-out;
    .drawer-container {
      position: fixed;
      background: #fff;
      top: 0;
      right: 0;
      left: auto;
      height: 100%;
      transform-origin: center;
      transition: all 0.3s ease-in-out;
      z-index: 99999;
      .drawer-title {
        font-size: 16px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: rgba(51, 51, 51, 1);
        line-height: 22px;
        padding: 16px 20px;
      }
      .drawer-content {
        padding: 0 20px 50px;
        display: flex;
        align-items: center;
        input {
          width: 110px;
          height: 30px;
          background: rgba(247, 247, 247, 1);
          border-radius: 17px;
          text-align: center;
          font-size: 14px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: rgba(153, 153, 153, 1);
          line-height: 30px;
        }
        .reduce {
          margin: 0 5px;
          width: 9px;
          height: 1px;
          background: #999999;
        }
      }
      .btnbox {
        position: fixed;
        bottom: 30px;
        padding: 0 20px;
        box-sizing: border-box;
        &.iphonex {
          bottom: 60px;
        }
        .safearea {
          width: 100%;
          display: flex;
          align-items: center;
          justify-content: space-between;
          .reset {
            width: 114px;
            height: 36px;
            border-radius: 20px;
            border: 1px solid rgba(232, 232, 232, 1);
            text-align: center;
            font-size: 14px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: rgba(51, 51, 51, 1);
            line-height: 36px;
            margin-right: 11px;
          }
          .screen {
            width: 114px;
            height: 36px;
            background: linear-gradient(
              270deg,
              rgba(232, 58, 94, 1) 0%,
              rgba(255, 168, 70, 1) 100%
            );
            border-radius: 20px;
            text-align: center;
            font-size: 14px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: rgba(255, 255, 255, 1);
            line-height: 36px;
          }
        }
      }
    }
  }
  .drawer-show {
    visibility: visible;
    opacity: 1;
  }
}
</style>